package com.hunan.api.catv.service.impl.tencent_cloud.cos;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.exception.CosClientException;
import com.qcloud.cos.exception.CosServiceException;
import com.qcloud.cos.model.ObjectMetadata;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import com.qcloud.cos.region.Region;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

@Service
public class CosService {

    public static Logger log = LoggerFactory.getLogger(CosService.class);

    public String upload(MultipartFile file, String secretId, String secretKey, String region, String bucketName, String key, String fileSuffix) {
        COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
        ClientConfig clientConfig = new ClientConfig(new Region(region));
        COSClient cosclient = new COSClient(cred, clientConfig);
        //String bucketName = "zbx-1301535236";
        //String key = "aaa/aaa.jpg";
        //File localFile = new File("/Users/zbx/Downloads/testPic/318K.jpg");
        try {
//        	 InputStream input = new FileInputStream(localFile);
            InputStream input = file.getInputStream();
            ObjectMetadata objectMetadata = new ObjectMetadata();
            objectMetadata.setContentLength(file.getSize());
            if ("jpg".equals(fileSuffix.toLowerCase()) || "jpeg".equals(fileSuffix.toLowerCase())) {
                objectMetadata.setContentType("image/jpeg");
            }
            if ("png".equals(fileSuffix.toLowerCase())) {
                objectMetadata.setContentType("image/png");
            }
            if ("gif".equals(fileSuffix.toLowerCase())) {
                objectMetadata.setContentType("image/gif");
            }
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, input, objectMetadata);
            //putObjectRequest.setStorageClass(StorageClass.Standard);
            PutObjectResult putObjectResult = cosclient.putObject(putObjectRequest);
            return putObjectResult.getETag();
        } catch (CosServiceException e) {
            log.error("[Cos-Error:" + e.toString() + "]");
        } catch (CosClientException e) {
            log.error("[Cos-Error:" + e.toString() + "]");
        } catch (IOException e) {
            log.error("[Cos-Error:" + e.toString() + "]");
        } finally {
            cosclient.shutdown();
        }
        return "";
    }

//	public static void main(String[] args) throws FileNotFoundException {
//		COSCredentials cred = new BasicCOSCredentials("AKIDKm0ymLnka9ZmmfOzUlthMm1GLAlARPDl", "yiOp0jGbrrkPtDIF3yoQwCDl47uRejZI");
//        ClientConfig clientConfig = new ClientConfig(new Region("ap-guangzhou"));
//        COSClient cosclient = new COSClient(cred, clientConfig);
//        String bucketName = "catv-1301401569";
//        String key = "2020/aaa.jpg";
//        File localFile = new File("/Users/zbx/Downloads/2.jpg");
//        InputStream input = new FileInputStream(localFile);
//        ObjectMetadata objectMetadata = new ObjectMetadata();
//        objectMetadata.setContentLength(localFile.length());
//       	objectMetadata.setContentType("image/jpg");
//        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, input, objectMetadata);
//        //putObjectRequest.setStorageClass(StorageClass.Standard);
//        PutObjectResult putObjectResult = cosclient.putObject(putObjectRequest);
//	}
}
